﻿using System.Collections;
using System.Collections.Generic;
using TinyTeam.UI;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class TalkUI : TTUIPage
{
    private Image leftImg;
    private Text txt;
    private Image rightImg;
    private TalkUIData uiData;
    private TalkData td;
    private Coroutine cor;
    private Button btn;
    private Animator leftAm;
    private Animator rightAm;


    private static bool isNeedShowTip = true;

    public TalkUI() : base(UIType.Normal, UIMode.DoNothing, UICollider.None)
    {
        uiPath = "UI/TalkUI";
    }

    public override void Awake(GameObject go)
    {
        leftAm = go.transform.Find("bg/leftImg").GetComponent<Animator>();
        rightAm = go.transform.Find("bg/rightImg").GetComponent<Animator>();
        leftImg = go.transform.Find("bg/leftImg").GetComponent<Image>();
        txt = go.transform.Find("bg/Text").GetComponent<Text>();
        rightImg = go.transform.Find("bg/rightImg").GetComponent<Image>();
        btn = go.transform.Find("bg").GetComponent<Button>();
        btn.onClick.AddListener(() =>
        {
            //判定有没有下一句话
            if (td.nextId == 0)
            {
                ClosePage<TalkUI>();
                uiData.cb?.Invoke();
            }
            else
            {
                //如果有就继续执行
                PT(td.nextId);
            }
        });
    }

    public override void Refresh()
    {
        if (isNeedShowTip)
        {
            TipUIData td = new TipUIData() {cb = null, content = "点击对话框进行下一步"};
            ShowPage<TipUI>(td);
            isNeedShowTip = false;
        }

        uiData = (TalkUIData) data;
        PT(uiData.id);
    }

    private void PT(int id)
    {
        btn.enabled = false;
        td = GameManager.instance.TalkData[id];
        if (cor != null)
        {
            Util.instance.StopCoroutine(cor);
        }

        if (td.npcType == 1)
        {
            leftImg.gameObject.SetActive(true);
            rightImg.gameObject.SetActive(false);
            leftAm.runtimeAnimatorController =
                Resources.Load<RuntimeAnimatorController>("anim/talkam/" + td.amCtrlName);
            leftAm.SetInteger("State", 0);
            leftImg.transform.rotation = Quaternion.identity;
        }
        else if (td.npcType == 2)
        {
            leftImg.gameObject.SetActive(false);
            rightImg.gameObject.SetActive(true);
            rightAm.runtimeAnimatorController =
                Resources.Load<RuntimeAnimatorController>("anim/talkam/" + td.amCtrlName);
            rightAm.SetInteger("State", 0);
            rightImg.transform.rotation = Quaternion.Euler(0, 180, 0);
        }

        cor = Util.instance.Typewriter(td.content, 0.05f, txt, () =>
        {
            btn.enabled = true;
            if (td.npcType == 1)
            {
                leftAm.SetInteger("State", 1);
            }
            else
            {
                rightAm.SetInteger("State", 1);
            }
        });
    }
}